package megrez.oss.local.domain;

import java.io.Serializable;

import org.springframework.util.StringUtils;

import lombok.Getter;
import megrez.oss.common.Pair;

/**
 * 文件二级路径
 *  
 * @author Lucky Yang
 * @since 2.6.9
 */
@Getter
public class FilePathPair implements Serializable {
    public static final String SEP_STRING = ",";

    private final String first;
    private final String second;

    private FilePathPair(String first, String second) {
        this.first = first;
        this.second = second;
    }

    @Override
    public String toString() {
        return first + SEP_STRING + second;
    }

    /**
     * 字符串转化成路径，如：'folder1,folder2'
     * @param path
     * @return
     */
    public static FilePathPair withString(final String path) {
        if (!StringUtils.hasText(path)) {
            throw new IllegalArgumentException("File path is empty");
        }

        String firstTmp = "";
        String secondTmp = "";
        String[] paths = path.split(SEP_STRING);
        if (paths.length > 1) {
            firstTmp = paths[0];
        }

        if (paths.length > 1) {
            secondTmp = paths[1];
        }

        return new FilePathPair(firstTmp, secondTmp);
    }

    public static FilePathPair withPair(final Pair<String, String> pair) {
        return new FilePathPair(pair.value0, pair.value1);
    }
}
